package com.ruoyi.web.controller.oss;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oss.domain.SysOss;
import com.ruoyi.oss.service.ISysOssService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * OSS对象存储Controller
 *
 * @author ruoyi
 * @date 2023-02-09
 */
@RestController
@RequestMapping("/oss/file")
public class SysOssController extends BaseController
{
    @Autowired
    private ISysOssService sysOssService;

    /**
     * 查询OSS对象存储列表
     */
    @PreAuthorize("@ss.hasPermi('oss:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOss sysOss)
    {
        startPage();
        QueryWrapper<SysOss> sysOssQueryWrapper = new QueryWrapper<>(sysOss);
        List<SysOss> list = sysOssService.list(sysOssQueryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出OSS对象存储列表
     */
    @PreAuthorize("@ss.hasPermi('oss:file:export')")
    @Log(title = "OSS对象存储", businessType = BusinessType.EXPORT)
    @GetMapping("/download/{ossId}")
    public void download(HttpServletResponse response, @PathVariable Long ossId)
    {
        try {
            sysOssService.download(ossId, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取OSS对象存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('oss:file:query')")
    @GetMapping(value = "/{ossId}")
    public AjaxResult getInfo(@PathVariable("ossId") Long ossId)
    {
        return success(sysOssService.getById(ossId));
    }

    /**
     * 新增OSS对象存储
     */
    @PreAuthorize("@ss.hasPermi('oss:file:add')")
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.save(sysOss));
    }

    @PreAuthorize("@ss.hasPermi('oss:file:add')")
    @Log(title = "OSS对象存储", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = sysOssService.upload(file);
        return success(oss);
    }

    /**
     * 修改OSS对象存储
     */
    @PreAuthorize("@ss.hasPermi('oss:file:edit')")
    @Log(title = "OSS对象存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.updateById(sysOss));
    }

    /**
     * 删除OSS对象存储
     */
    @PreAuthorize("@ss.hasPermi('oss:file:remove')")
    @Log(title = "OSS对象存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return toAjax(sysOssService.removeByIds(list));
    }
}
